#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define PORT 9999
#define BUF_SIZE 1024

int main() {
    int server_socket, client_socket;
    struct sockaddr_in server_addr, client_addr;
    socklen_t client_addr_size;
    char buf[BUF_SIZE];
    int read_size;

    server_socket = socket(PF_INET, SOCK_STREAM, 0);
    if(server_socket == -1) {
        perror("socket()");
        exit(1);
    }

    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port = htons(PORT);

    if(bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
        perror("bind()");
        exit(1);
    }

    if(listen(server_socket, 5) == -1) {
        perror("listen()");
        exit(1);
    }

    client_addr_size = sizeof(client_addr);
    client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_addr_size);
    if(client_socket == -1) {
        perror("accept()");
        exit(1);
    }

    int total_size = 0;
    while(total_size < BUF_SIZE * 700) {
        read_size = read(client_socket, buf, BUF_SIZE);
        if (read_size > 0) {
            total_size += read_size;
        }
        printf("total_size = %d\n", total_size);
    }


    close(client_socket);
    close(server_socket);
    return 0;
}
